package com.lcifn.middleware.rpc.transport.coder;

import com.lcifn.middleware.rpc.transport.command.Header;
import com.lcifn.middleware.rpc.transport.command.ResponseHeader;
import io.netty.buffer.ByteBuf;

import java.nio.charset.StandardCharsets;

/**
 * 客户端响应解码
 * @author lichao
 * @date 2021/2/1 16:28
 */
public class ClientResponseDecoder extends CommandDecoder {

    @Override
    protected Header decodeHeader(ByteBuf byteBuf) {
        int requestId = byteBuf.readInt();
        int type = byteBuf.readInt();
        int version = byteBuf.readInt();
        int code = byteBuf.readInt();
        int errorLength = byteBuf.readInt();
        byte [] errorBytes = new byte[errorLength];
        byteBuf.readBytes(errorBytes);
        String error = new String(errorBytes, StandardCharsets.UTF_8);
        return new ResponseHeader(new Header(requestId, type, version), code, error);
    }
}
